package com.payment.ktb.activity.main4;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.payment.ktb.Interface.HttpCallbackListener;
import com.payment.ktb.R;
import com.payment.ktb.activity.BaseActivity;
import com.payment.ktb.adapter.AgentMsgAdapter;
import com.payment.ktb.constants.HttpActions;
import com.payment.ktb.constants.HttpUrls;
import com.payment.ktb.model.AgentMsgEntity;
import com.payment.ktb.utils.AlertDialogUtils;
import com.payment.ktb.utils.HttpUtils;
import com.payment.ktb.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by admin on 2017/9/29.
 */

public class AgentMsgActivity extends BaseActivity {


    @BindView(R.id.prlv_agentmsg)
    PullToRefreshListView prlv_agentmsg;
    private int page = 0;
    private List<AgentMsgEntity> listDatas = new ArrayList<>();
    private AgentMsgAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_agentmsg);
        ButterKnife.bind(this);
        initTitle("消息");
        prlv_agentmsg.setMode(PullToRefreshBase.Mode.BOTH);
        prlv_agentmsg.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                // 设置下拉刷新加载文本
                prlv_agentmsg.getLoadingLayoutProxy(true, false).setPullLabel("下拉刷新...");
                prlv_agentmsg.getLoadingLayoutProxy(true, false).setReleaseLabel("放开刷新...");
                prlv_agentmsg.getLoadingLayoutProxy(true, false).setRefreshingLabel("正在加载...");
                //下拉刷新
                pullDownToRefresh();
            }
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                // 设置上拉加载文本
                prlv_agentmsg.getLoadingLayoutProxy(false, true).setPullLabel("上拉刷新...");
                prlv_agentmsg.getLoadingLayoutProxy(false, true).setReleaseLabel("放开刷新...");
                prlv_agentmsg.getLoadingLayoutProxy(false, true).setRefreshingLabel("正在加载...");
                pullUptoLoad();
            }
        });

        prlv_agentmsg.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                getAgentMsgDetail(listDatas.get(i-1).getId());
            }
        });

        initData();
    }

    private void initData() {

        //data
        if (TextUtils.isEmpty(getIntent().getStringExtra("records"))) {
            ToastUtils.showToast("没有更多数据！");
        }else {
            listDatas = new Gson().fromJson(getIntent().getStringExtra("records"), new TypeToken<List<AgentMsgEntity>>() {}.getType());
        }
        //adapter
        adapter = new AgentMsgAdapter(mContext,listDatas);
        //bind
        prlv_agentmsg.setAdapter(adapter);

    }


    //下拉刷新
    public void pullDownToRefresh() {

        progressDialogUtils.showProgress();
        page = 0;
        //构造传输参数
        Map<String, String> map = new HashMap<String,String>();

        map.put("page", String.valueOf(page));

        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.OrgMsgList, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {
                progressDialogUtils.dismissProgress();
                prlv_agentmsg.onRefreshComplete();
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    //data
                    if (TextUtils.isEmpty(jsonObject.optString("records"))) {
                        ToastUtils.showToast("没有更多数据！");
                    }else {
                        listDatas = new Gson().fromJson(jsonObject.optString("records"), new TypeToken<List<AgentMsgEntity>>() {}.getType());
                        //adapter
                        adapter = new AgentMsgAdapter(mContext,listDatas);
                        //bind
                        prlv_agentmsg.setAdapter(adapter);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }
            }

            @Override
            public void onError(VolleyError volleyError) {
                progressDialogUtils.dismissProgress();
                prlv_agentmsg.onRefreshComplete();
                AlertDialogUtils.showAlertDialog(mContext, volleyError.getMessage());
            }
        });
    }




    //上拉加载更多
    public void pullUptoLoad() {

        progressDialogUtils.showProgress();
        page = page + 1;
        //构造传输参数
        Map<String, String> map = new HashMap<String,String>();
        //1.系统消息，2活动消息
        map.put("page", String.valueOf(page));

        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.OrgMsgList, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {
                progressDialogUtils.dismissProgress();
                prlv_agentmsg.onRefreshComplete();
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    //data
                    if (jsonObject.has("records")){
                        List<AgentMsgEntity> temp = new Gson().fromJson(jsonObject.getJSONArray("records").toString(), new TypeToken<List<AgentMsgEntity>>() {}.getType());
                        listDatas.addAll(temp);
                        adapter.notifyDataSetChanged();
                    }else {
                        ToastUtils.showToast("没有更多数据啦！");
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }
            }

            @Override
            public void onError(VolleyError volleyError) {
                progressDialogUtils.dismissProgress();
                prlv_agentmsg.onRefreshComplete();
                AlertDialogUtils.showAlertDialog(mContext, volleyError.getMessage());
            }
        });
    }


    //获取代理商消息通知详情
    public void getAgentMsgDetail(int id) {

        progressDialogUtils.showProgress();

        //构造传输参数
        Map<String, String> map = new HashMap<String,String>();
        map.put("id", String.valueOf(id));

        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.OrgMsgDetail, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {
                progressDialogUtils.dismissProgress();
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    Intent intent = null;
                    if (jsonObject.optInt("noticeType") == 1) {
                        intent = new Intent(mContext,AgentMsgDetailApplyingActivity.class);
                    }else if (jsonObject.optInt("noticeType") == 2){
                        intent = new Intent(mContext,AgentMsgDetailApplyDoneActivity.class);
                    }
                    intent.putExtra("noticeContent",jsonObject.optString("noticeContent"));
                    startActivity(intent);


                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }
            }

            @Override
            public void onError(VolleyError volleyError) {
                progressDialogUtils.dismissProgress();
                AlertDialogUtils.showAlertDialog(mContext, volleyError.getMessage());
            }
        });
    }
}
